package cn.achao.csr.chat.dao;

import cn.achao.csr.api.model.entity.ChatContact;
import cn.achao.csr.base.resp.CursorPageBaseResp;
import cn.achao.csr.chat.mapper.ChatContactMapper;
import cn.achao.csr.mybatis.req.MPBaseCursorPageReq;
import cn.achao.csr.tool.utils.CursorUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatContactDao extends ServiceImpl<ChatContactMapper, ChatContact> {

    private final CursorUtils cursorUtils;
    /**
     * 获取用户会话列表
     * @param uid
     * @param request
     * @return
     */
    public CursorPageBaseResp<ChatContact> getContactPage(Long uid, MPBaseCursorPageReq request) {
        return cursorUtils.getCursorPageByMysql(this, request, wrapper -> {
            wrapper.eq(ChatContact::getUserId, uid);
        }, ChatContact::getActiveTime);
    }

    public List<ChatContact> getByRoomIds(List<Long> roomIds, Long uid) {
        return lambdaQuery()
                .in(ChatContact::getRoomId, roomIds)
                .eq(ChatContact::getUserId, uid)
                .list();
    }

    public ChatContact getUserRoomContact(Long userId, Long roomId) {
        return lambdaQuery()
                .in(ChatContact::getRoomId, roomId)
                .eq(ChatContact::getUserId, userId)
                .one();
    }
}

